<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="zh">
<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <link th:href="@{/css/font.css}" rel="stylesheet"/>
    <link th:href="@{/css/xadmin.css}" rel="stylesheet"/>
    <script th:src="@{/lib/layui/layui.js}" charset="utf-8"></script>
    <script th:src="@{/js/xadmin.js}" type="text/javascript" charset="utf-8"></script>
    <script th:inline="javascript"> var ctx = [[@{/}]]; </script>
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <form class="layui-form layui-col-space5">
                        <div class="layui-form-item">

                                <div class="layui-inline">
                                    <label class="layui-form-label">角色名</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="roleName" id="roleName" placeholder="请输入角色名称" autocomplete="off"
                                           class="layui-input">
                                    </div>
                                </div>
                            <!--<div class="layui-inline">-->
                                <!--<label class="layui-form-label">角色状态</label>-->
                                <!--<div class="layui-input-inline">-->
                                    <!--<input type="text" name="roleStatus" id="roleStatus" placeholder="请输入角色状态" autocomplete="off" class="layui-input">-->
                                <!--</div>-->
                            <!--</div>-->
                            <div class="layui-inline">
                                <label class="layui-form-label">角色状态</label>
                                <div class="layui-input-inline">
                                    <select name="roleStatus" id="roleStatus" lay-search="">
                                        <option value="">所有</option>
                                        <option value="0">正常</option>
                                        <option value="1">停用</option>
                                    </select>
                                </div>
                            </div>

                                <div class="layui-inline">
                                    <label class="layui-form-label">创建时间</label>
                                    <div class="layui-input-inline">
                                        <input type="text" class="layui-input" id="createTime" placeholder="请选择时间" autocomplete="off">
                                    </div>
                                </div>

                                <div class="layui-inline layui-show-xs-block">
                                    <a class="layui-btn" lay-submit="" lay-filter="sreach" id="sreach"><i
                                            class="layui-icon">&#xe615;</i></a>
                                </div>
                        </div>

                    </form>
                </div>

                <div class="layui-card-body ">
                    <!--<script type="text/html" id="toolbarDemo">-->
                        <!--<div class="layui-btn-container">-->
                            <!--<button class="layui-btn layui-btn-sm" lay-event="addUser">添加用户</button>-->
                        <!--</div>-->
                    <!--</script>-->
                    <table id="demo" class="layui-table layui-form" lay-filter="test"></table>
                    <script type="text/html" id="barDemo">
                        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>

                    </script>
                    <script type="text/html" id="switchTpl">
                        <!-- 这里的 checked 的状态只是演示 -->
                        <input type="checkbox" name="status" value="{{d.status}}"  lay-skin="switch" lay-text="|" lay-filter="sexDemo" {{ d.status == 0  ?'checked' : '' }}>
                    </script>
                </div>


            </div>
        </div>
    </div>
</div>
</body>

<script>
    layui.config({
        version: '1559794877226' //为了更新 js 缓存，可忽略
    });
    layui.use(['laydate', 'layer', 'table'], function () {
        var laydate = layui.laydate //日期
            ,
            layer = layui.layer //弹层
            ,
            table = layui.table //表格
            ,
            form = layui.form

        //日期范围
        laydate.render({
            elem: '#createTime'
            ,range: '~'
            ,done: function(value, date, endDate){
            if(date.year == undefined){
                startTime = '';
                endTime = '';
            }else{
                startTime = date.year+'-'+date.month+'-'+date.date;
	            endTime = endDate.year+'-'+endDate.month+'-'+endDate.date;
            }
	      }

            });

              var statusTpl = function (d) { // 参数d是当前行数据
	                // if else判断
                    if(d.status == 0){
	                    return `<input type="checkbox" lay-skin="switch" value="${d.roleId}" lay-filter="statusDemo" lay-text="可用|禁用" checked>`;
                     }else{
	                    return `<input type="checkbox" lay-skin="switch" value="${d.roleId}" lay-filter="statusDemo" lay-text="可用|禁用"> `;
                     }
                };
        //加载表格
        table.render({
            elem: '#demo',
            height: 312,
            url: '/getRoleList'
            ,toolbar: 'default'
            ,id: 'testReload'
            ,page: true
            //,totalRow: true
            ,cols: [
                [{
                    checkbox: true
                }, {
                    field: 'roleId',
                    title: '角色编号',
                    sort: true
                }, {
                    field: 'roleName',
                    title: '角色名称',
                    sort: true
                }, {
                    field: 'roleKey',
                    title: '英文名称',
                    sort: true
                }, {
                    field: 'status',
                    title: '状态'
                    ,templet: statusTpl

                }, {
                    field: 'createTime',
                    title: '创建时间',
                     width: 165,
                    sortable: true
                }, {
                    title: '操作',
                    align: 'center',
                    width: 165,
                    toolbar: "#barDemo"
                }]
            ]
        });

          $('#sreach').on('click', function () {
            // 搜索条件
            var roleName = $('#roleName').val();
            var roleStatus = $('#roleStatus').val();
            if(typeof startTime == "undefined"){
                startTime = '';
                endTime = '';
            }

            table.reload('testReload', {
                method: 'post',
                where: {
                    'roleName': roleName,
                    'status':roleStatus,
                    'params[beginTime]':startTime,
                    'params[endTime]':endTime
                }
                , page: {
                    curr: 1
                }
            });
        });


        //监听行工具事件
        table.on('tool(test)', function (obj) {
            var data = obj.data //获得当前行数据
                ,
                layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    obj.del(); //删除对应行（tr）的DOM结构
                    //layer.close(index);
                    //向服务端发送删除指令
                     $.ajax({   
                url:'/deleteRole',       
                method:'post',       
                data:{'ids':data.roleId},        
                 dataType:'JSON',         
                  success:function(res){      
                            console.log(res); 
                    if(res.code == '0'){
                                //layer.msg('删除成功');
                                layer.alert("删除成功", {icon: 8},
                                function() {
                                    // 刷新当前窗口
                                    location.reload();
                                });   
                 } else{
                                layer.msg('删除失败');
                                 location.reload();
                              }                        
             }              
             }) ;  

                });
            } else if (layEvent === 'edit') {
                //layer.msg('编辑操作');
                var roleId = data.roleId;
                xadmin.open('编辑角色',`/editRolePage/${roleId}`,700,500);
            }
        });

//监听状态操作
  form.on('switch(statusDemo)', function(data){
        if(data.elem.checked){
            layer.confirm('确定启用状态吗', function (index) {
                 $.ajax({   
                url:'/system/role/editRole',       
                method:'post',       
                data:{'roleId':data.value,"status": 0},        
                 dataType:'JSON',         
                  success:function(res){      
                            console.log(res); 
                    if(res.code == '0'){
                                layer.alert("更改成功", {icon: 8},
                                function() {
                                    // 刷新当前窗口
                                    location.reload();
                                });   
                 } else{
                                layer.msg('删除失败');
                                 location.reload();
                              }                        
             }              
             }) ;  
            });
        }else{
            layer.confirm('确定停用状态吗', function (index) {
                 $.ajax({   
                url:'/system/role/editRole',       
                method:'post',       
                data:{'roleId':data.value,"status": 1},        
                 dataType:'JSON',         
                  success:function(res){      
                            console.log(res); 
                    if(res.code == '0'){
                                layer.alert("更改成功", {icon: 8},
                                function() {
                                    // 刷新当前窗口
                                    location.reload();
                                });   
                 } else{
                                layer.msg('删除失败');
                                 location.reload();
                              }                        
             }              
             }) ;  
            });
        }
  });


  //监听头工具栏事件
  table.on('toolbar(test)', function(obj){
    var checkStatus = table.checkStatus(obj.config.id)
    ,data = checkStatus.data; //获取选中的数据
    switch(obj.event){
      case 'add':
        //layer.msg('添加');
        xadmin.open('添加角色','/addRolePage',700,500);
      break;
      case 'update':
        if(data.length === 0){
          layer.msg('请选择一行');
        } else if(data.length > 1){
          layer.msg('只能同时编辑一个');
        } else {
          var roleId = data[0].roleId;
          xadmin.open('编辑角色',`/editRolePage/${roleId}`,700,500);

        }
      break;
      case 'delete':
        if(data.length === 0){
          layer.msg('请选择一行');
        } else {
            //console.log(data);
            layer.confirm('确认要删除选中的' + data.length + '条数据吗?', function(index){

                console.log(data)
                var arrId = [];
                data.forEach(function(currentValue){
                    arrId.push(currentValue.roleId);
                });
                var ids = arrId.join(",")
                console.log(ids);

                $.ajax({   
                url:'/deleteRole',       
                method:'post',       
                data:{'ids':ids},        
                 dataType:'JSON',         
                  success:function(res){      
                            console.log(res); 
                    if(res.code == '0'){
                                //layer.msg('删除成功');
                                layer.alert("删除成功", {icon: 8},
                                function() {
                                    // 刷新当前窗口
                                    location.reload();
                                });   
                 } else{
                                layer.msg('删除失败');
                                 location.reload();
                              }                        
             }              
             }) ;  

            });
        }
      break;
    };
  });



    });
</script>

</html>